package com.example.appp;

/**
 * Created with Eclipse
 * User: Serdyuk Evgen BY "name?"
 * Date: 21.10.12
 * Time: 04:26
 */

import java.util.Timer;
import java.util.TimerTask;

import com.example.appp.Player.side;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

public class GameView extends View {

    private static final int START_POSITION_Y = 17;
    private static final int START_POSITION_X = 7;
    private static final int BOT_POSITION_X = 6;
    private static final int BOT_POSITION_Y = 11;
    private static final int SIZE_320X240 = 20;
    private static final int SIZE_480X320 = 30;
    Timer mainTimer = new Timer();
    Timer additionalTimer = new Timer();
    Game mainGame = new Game(getContext());

    GameMap gameMap;
    Spirit blinky;
    Spirit pinky;
    Spirit clyde;
    Spirit inky;
    Player pacman;

    boolean onClose;
    int time = 0;
    Rect rectanglePacman = new Rect();

    int gameWidth;
    int gameHeight;

    double ang;
    boolean IsCreate = false;

    public GameView(Context context) {
        super(context);

        mainTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                if (mainGame.isGame) {
                    pacman.onMove(gameMap.map, gameMap.size);
                    rectanglePacman.set(pacman.positionX, pacman.positionY,
                            pacman.positionX + gameMap.size, pacman.positionY
                                    + gameMap.size);
                    gameMap.interSecrRect(rectanglePacman);
                }
            }
        }, 0, 100);

        additionalTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                if (mainGame.isGame) {

                    gameMap.findPath(pacman.IsRight, pacman.IsLeft,
                            pacman.IsUp, pacman.IsDown, pacman.positionX,
                            pacman.positionY);
                    pinky.findPathBlinky(gameMap.inverseMap, gameMap.size);
                    gameMap.findPath(pacman.positionX, pacman.positionY);
                    blinky.findPathBlinky(gameMap.inverseMap, gameMap.size);
                    clyde.findPathClyde(gameMap.map, gameMap.size,
                            pacman.positionX, pacman.positionY);
                    inky.findPathInky(gameMap.map, gameMap.size);
                    inky.onMove(gameMap.map, gameMap.size);
                    clyde.onMove(gameMap.map, gameMap.size);
                    pinky.onMove(gameMap.map, gameMap.size);
                    blinky.onMove(gameMap.map, gameMap.size);

                    mainGame.isDead(rectanglePacman, blinky.rectangleSpirit,
                            pinky.rectangleSpirit, inky.rectangleSpirit,
                            clyde.rectangleSpirit);
                    if (mainGame.isDead) {
                        blinky = new Spirit(getContext(), gameMap.size,
                                BOT_POSITION_X + 1, BOT_POSITION_Y);
                        pinky = new Spirit(getContext(), gameMap.size,
                                BOT_POSITION_X + 2, BOT_POSITION_Y);
                        clyde = new Spirit(getContext(), gameMap.size,
                                BOT_POSITION_X + 3, BOT_POSITION_Y);
                        inky = new Spirit(getContext(), gameMap.size,
                                BOT_POSITION_X + 4, BOT_POSITION_Y);

                        time = 0;
                        gameMap.map[7][12] = 0;
                        gameMap.map[8][12] = 0;
                        pacman.positionX = START_POSITION_X * gameMap.size;
                        pacman.positionY = START_POSITION_Y * gameMap.size;
                        pacman.life--;

                        mainGame.isDead = false;
                    }

                    if (!onClose) {
                        time++;
                    }
                }
                if (!onClose && time == 5) {
                    gameMap.map[7][12] = 200;
                    gameMap.map[8][12] = 200;
                    onClose = true;
                }
            }
        }, 0, 110);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!IsCreate) {
            gameHeight = getHeight();
            gameWidth = getWidth();

            gameMap = new GameMap(getContext(), gameWidth);
            blinky = new Spirit(getContext(), gameMap.size, 6, 11);
            pinky = new Spirit(getContext(), gameMap.size, 9, 11);
            clyde = new Spirit(getContext(), gameMap.size, 7, 11);
            inky = new Spirit(getContext(), gameMap.size, 8, 11);
            pacman = new Player(getContext(), gameMap.size);
            gameMap.pointMap();
            IsCreate = true;
        }

        if (mainGame.isGame) {
            pacman.onLoadImages(gameMap.size);

            blinky.onLoadImageaBlinky();
            pinky.onLoadImageaPinky();
            clyde.onLoadImageaClyde();
            inky.onLoadImageaInky();

            // GM.DrawGrid(canvas); // special for debug
            gameMap.onDraw(canvas);
            pacman.onDraw(canvas);
            blinky.onDraw(canvas);
            pinky.onDraw(canvas);
            clyde.onDraw(canvas);
            inky.onDraw(canvas);

        } else {
            mainGame.onDraw(canvas);

        //    canvas.drawText(Integer.toString(gameWidth), 100, 100, new Paint());// special for debug
        }
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();
        if (!mainGame.isGame) {
            if (gameMap.size == SIZE_480X320) {
                if (touchY > 160 && touchY < 240)
                    mainGame.newGame();
                if (touchY > 480 && touchY < 560)
                    mainGame.finishGame();
            }
            if (gameMap.size == SIZE_320X240) {
                if (touchY > 106 && touchY < 160)
                    mainGame.newGame();
                if (touchY > 320 && touchY < 374)
                    mainGame.finishGame();
            }
        } else {

            if (gameMap.size == SIZE_480X320) {
                if (touchX < 110 * 2 && touchY > 160 * 2 && touchY < 320 * 2)
                    pacman.way = side.LEFT;
                if (touchX > 220 * 2 && touchX < 320 * 2 && touchY > 160 * 2 && touchY < 320 * 2)
                    pacman.way = side.RIGHT;
                if (touchX > 80 * 2 && touchX < 240 * 2 && touchY < 160 * 2)
                    pacman.way = side.UP;
                if (touchX > 80 * 2 && touchX < 240 * 2 && touchY > 320 * 2 && touchY < 480 * 2)
                    pacman.way = side.DOWN;

            }
            if (gameMap.size == SIZE_320X240) {
                if (touchX < 110 && touchY > 160 && touchY < 320)
                    pacman.way = side.LEFT;
                if (touchX > 220 && touchX < 320 && touchY > 160 && touchY < 320)
                    pacman.way = side.RIGHT;
                if (touchX > 80 && touchX < 240 && touchY < 160)
                    pacman.way = side.UP;
                if (touchX > 80 && touchX < 240 && touchY > 320 && touchY < 480)
                    pacman.way = side.DOWN;
            }
        }
        return true;

    }

}
